KTrader Python API 文档¶
ktrader_python 模块API文档¶
KTrader Python 数据结构定义以及API
- class ktrader_python.tick
基类:
object行情数据
- instrument_id: str
合约ID
- timestamp: int
时间戳(Unix纳秒)
- last_price: float
最新价格
- highest_price: float
最高价格
- lowest_price: float
最低价格
- volume_delta: int
新增成交
- open_interest_delta: int
新增仓位
- turnover_delta: int
新增成交量
- bid_price: List[float]
买价
- ask_price: List[float]
卖价
- bid_volume: List[int]
买量
- ask_volume: List[int]
卖量
- class ktrader_python.product_class_enum(value)
基类:
enum.Enum产品类型
- undefined = ('undefined', '未定义')
- futures = ('futures', '期货')
- options = ('options', '期货期权')
- combination = ('combination', '组合')
- spot = ('spot', '即期')
- EFP = ('EFP', '期转现')
- spot_option = ('spot_option', '现货期权')
- TAS = ('TAS', 'TAS合约')
- MI = ('MI', '金属指数')
- class ktrader_python.position_type_enum(value)
基类:
enum.Enum持仓类型
- undefined = ('undefined', '未定义')
- net = ('net', '净持仓')
- gross = ('gross', '综合持仓')
- class ktrader_python.position_date_type_enum(value)
基类:
enum.Enum持仓日期类型
- undefined = ('undefined', '未定义')
- use_history = ('use_history', '使用历史持仓')
- no_use_history = ('no_use_history', '不使用历史持仓')
- class ktrader_python.options_type_enum(value)
基类:
enum.Enum期权类型
- undefined = ('undefined', '未定义')
- call = ('call', '看涨')
- put = ('put', '看跌')
- class ktrader_python.combination_type_enum(value)
基类:
enum.Enum组合类型
- undefined = ('undefined', '未定义')
- future = ('future', '期货组合')
- BUL = ('BUL', '垂直价差BUL')
- BER = ('BER', '垂直价差BER')
- STD = ('STD', '跨式组合')
- STG = ('STG', '宽跨式组合')
- PRT = ('PRT', '备兑组合')
- CAS = ('CAS', '时间价差组合')
- OPL = ('OPL', '期权对锁组合')
- BFO = ('BFO', '买备兑组合')
- BLS = ('BLS', '买入期权垂直价差组合')
- BES = ('BES', '卖出期权垂直价差组合')
- class ktrader_python.margin_algo_enum(value)
基类:
enum.Enum保证金算法
- undefined = ('undefined', '未定义')
- no_use_max_margin_side_algo = ('no_use_max_margin_side_algo', '不使用大额单边保证金算法')
- use_max_margin_side_algo = ('use_max_margin_side_algo', '使用大额单边保证金算法')
- class ktrader_python.direction_enum(value)
基类:
enum.Enum买卖方向
- buy = ('buy', '买')
- sell = ('sell', '卖')
- undefined = ('undefined', '未定义')
- class ktrader_python.offset_flag_enum(value)
基类:
enum.Enum开平标志
- undefined = ('undefined', '未定义')
- open = ('open', '开仓')
- close = ('close', '平仓')
- force_close = ('force_close', '强平')
- close_today = ('close_today', '平今')
- close_yesterday = ('close_yesterday', '平昨')
- force_off = ('force_off', '强减')
- local_force_close = ('local_force_close', '本地强平')
- class ktrader_python.time_condition_enum(value)
基类:
enum.Enum时间条件
- undefined = ('undefined', '未定义')
- instant_or_cancel = ('instant_or_cancel', '立即完成或者撤销')
- good_for_session = ('good_for_session', '本节有效')
- good_for_day = ('good_for_day', '当日有效')
- good_till_date = ('good_till_date', '指定日期前有效')
- good_till_cancel = ('good_till_cancel', '撤销前有效')
- good_for_aggr = ('good_for_aggr', '集合竞价有效')
- class ktrader_python.volume_condition_enum(value)
基类:
enum.Enum成交条件
- undefined = ('undefined', '未定义')
- any_volume = ('any_volume', '任何数量')
- minimum_volume = ('minimum_volume', '最小数量')
- all_volume = ('all_volume', '全部数量')
- class ktrader_python.order_status_enum(value)
基类:
enum.Enum报单状态
- undefined = ('undefined', '未定义')
- all_traded = ('all_traded', '全部成交')
- partial_traded_queueing = ('partial_traded_queueing', '部分成交还在队列中')
- partial_traded_not_queueing = ('partial_traded_not_queueing', '部分成交不在队列中')
- no_trade_queueing = ('no_trade_queueing', '未成交还在队列中')
- no_trade_not_queueing = ('no_trade_not_queueing', '未成交不在队列中')
- canceled = ('canceled', '已撤单')
- unknown = ('unknown', '未知')
- not_touched = ('not_touched', '尚未触发')
- touched = ('touched', '已触发')
- class ktrader_python.order_submit_status_enum(value)
基类:
enum.Enum报单提交状态
- undefined = ('undefined', '未定义')
- insert_submitted = ('insert_submitted', '已经提交')
- cancel_submitted = ('cancel_submitted', '撤单已经提交')
- modify_submitted = ('modify_submitted', '修改已经提交')
- accepted = ('accepted', '已经接受')
- insert_rejected = ('insert_rejected', '报单已经被拒绝')
- cancel_rejected = ('cancel_rejected', '撤单已经被拒绝')
- modify_rejected = ('modify_rejected', '改单已经被拒绝')
- class ktrader_python.position_direction_enum(value)
基类:
enum.Enum持仓多空方向
- undefined = ('undefined', '未定义')
- pd_net = ('pd_net', '净')
- pd_long = ('pd_long', '多头')
- pd_short = ('pd_short', '空头')
- class ktrader_python.position_date_enum(value)
基类:
enum.Enum持仓日期
- undefined = ('undefined', '未定义')
- today = ('today', '今日持仓')
- history = ('history', '历史持仓')
- class ktrader_python.trading_status_enum(value)
基类:
enum.Enum交易状态
- undefined = ('undefined', '未定义')
- before_trading = ('before_trading', '开盘前')
- no_trading = ('no_trading', '非交易')
- continous = ('continous', '连续交易')
- auction_ordering = ('auction_ordering', '集合竞价报单')
- auction_balance = ('auction_balance', '集合竞价价格平衡')
- auction_match = ('auction_match', '集合竞价撮合')
- closed = ('closed', '收盘')
- class ktrader_python.exchange_trading_status
基类:
object交易所交易状态
- exchange_id: str
交易所ID
- status: ktrader_python.trading_status_enum
交易状态
- class ktrader_python.trade_info
基类:
object成交信息
- timestamp: int
(Unix纳秒)
- instrument_id: str
合约ID
- broker_id: str
券商ID
- investor_id: str
投资者ID
- exchange_id: str
交易所ID
- order_ref: int
报单号
- trade_id: int
成交号
- direction: ktrader_python.direction_enum
成交方向
- offset: ktrader_python.offset_flag_enum
开平仓
- price: float
成交价格
- volume: int
成交量
- class ktrader_python.instrument_order_info
基类:
object合约信息
- id: str
合约ID
- name: str
合约名称
- product_id: str
品种ID
- exchange_id: str
交易所ID
- is_trading: bool
是否正在交易
- price_tick: float
最小价格单位
- volume_multiple: int
合约乘数
- expire_date: int
合约到期日(如20220222)
- default_last_price: float
默认最新价(启动时最新价)
- last_settlement_price: float
昨结算价
- upper_limit_price: float
涨停价
- lower_limit_price: float
跌停价
- min_market_volume: int
市价单最小报单量
- max_market_volume: int
市价单最大报单量
- min_limit_volume: int
限价单最小报单量
- max_limit_volume: int
限价单最大报单量
- long_margin_ratio_by_money: float
多头保证金率
- long_margin_ratio_by_volume: float
多头每手保证金
- short_margin_ratio_by_money: float
空头保证金率
- short_margin_ratio_by_volume: float
空头每手保证金
- open_commission_ratio_by_money: float
开仓手续费率
- open_commission_ratio_by_volume: float
开仓每手手续费
- close_commission_ratio_by_money: float
平仓手续费率
- close_commission_ratio_by_volume: float
平仓每手手续费
- close_today_commission_ratio_by_money: float
平今手续费率
- close_today_commission_ratio_by_volume: float
平今每手手续费
- order_commission_by_volume: float
报单手续费
- order_action_commission_by_volume: float
撤单手续费
- margin_algorithm: ktrader_python.margin_algo_enum
保证金算法
- position_type: ktrader_python.position_type_enum
持仓类型
- position_date_type: ktrader_python.position_date_type_enum
持仓日期类型
- product_class: ktrader_python.product_class_enum
品种类型
- option_type: ktrader_python.options_type_enum
期权类型
- combination_type: ktrader_python.combination_type_enum
组合类型
- strike_price: float
行权价格
- class ktrader_python.order_status
基类:
object报单状态
- instrument_id: str
合约ID
- front_id: int
前置ID
- session_id: int
会话ID
- order_ref: int
报单号
- order_sys_id: int
交易所报单号
- exchange_id: str
交易所ID
- status: ktrader_python.order_status_enum
报单状态
- submit_status: ktrader_python.order_submit_status_enum
报单提交状态
- direction: ktrader_python.direction_enum
报单方向
- offset: ktrader_python.offset_flag_enum
开平仓
- time_condition: ktrader_python.time_condition_enum
时间条件
- volume_condition: ktrader_python.volume_condition_enum
成交条件
- limit_price: float
限定价格
- volume_pending: int
未成交量
- volume_traded: int
已成交量
- frozen_margin: float
冻结保证金
- frozen_commission: float
冻结手续费
- order_commission: float
订单手续费
- cancel_requested: bool
是否已请求撤单
- error_code: int
错误码
- error_msg: str
错误信息
- class ktrader_python.instrument_trading_risk
基类:
object风险信息
- instrument_id: str
合约ID
- self_trade_count: int
自成交次数
- cancel_count: int
撤单数
- class ktrader_python.bar
基类:
objectK线数据
- instrument_id: str
合约ID
- start: int
开始时间(Unix Nano)
- end: int
结束时间(Unix Nano)
- open: float
开盘价
- high: float
最高价
- low: float
最低价
- close: float
收盘价
- turnover: float
成交额
- volume: int
成交量
- oi: int
开仓量
- class ktrader_python.trade_key
基类:
object成交主键, 用来唯一标志一笔成交
- instrument_id: str
合约ID
- trade_id: int
成交号
- direction: ktrader_python.direction_enum
方向
- class ktrader_python.order_update
基类:
object订单更新信息
- order: ktrader_python.order_status
订单信息
- has_trade: bool
是否包含成交
- trade: ktrader_python.trade_info
成交信息
- class ktrader_python.target_position_algorithm(value)
基类:
enum.Enum调仓算法
- undefined = ('undefined', '未定义')
- basic = ('basic', '基本')
- market_on_open = ('market_on_open', '开盘抢单')
- manual = ('manual', '手动')
- force_manual = ('force_manual', '强制手动')
- auto_active = ('auto_active', '自动主动价格(对价单)')
- auto_passive = ('auto_passive', '自动被动价格(挂价单)')
- class ktrader_python.position_target
基类:
object目标仓位
- instrument_id: str
合约ID
- algorithm: ktrader_python.target_position_algorithm
调仓算法
- target_pos: int
目标仓位
- desired_price: float
目标价格
- max_order_volume: int
最大下单量
- price_delta_threshold: int
更新订单价格偏差阈值(仅对自动价格有用)
- class ktrader_python.direction_entry
基类:
object持仓数据
- direction: ktrader_python.direction_enum
持仓方向
- total: int
总持仓
- today: int
今日持仓
- history: int
历史持仓
- total_cost: float
逐日总成本
- total_margin: float
总保证金
- frozen_open: int
冻结开仓手数
- frozen_close_today: int
冻结平今手数
- frozen_close_history: int
冻结平昨手数
- frozen_margin: float
冻结保证金
- class ktrader_python.instrument_summary
基类:
object合约持仓总结
- instrument_id: str
合约ID
- exchange_id: str
交易所ID
- position_profit: float
逐日持仓利润
- long_position: ktrader_python.direction_entry
多仓数据
- short_position: ktrader_python.direction_entry
空仓数据
- close_profit: float
逐日平仓利润
- total_margin: float
总保证金
- total_commission: float
总手续费
- total_order_commission: float
总报单手续费
- frozen_margin: float
冻结保证金
- frozen_commission: float
冻结手续费
- frozen_order_commission: float
冻结报单手续费
- net_pnl: float
净利润
- net_pos: int
净持仓
- class ktrader_python.position_entry
基类:
ktrader_python.trade_info逐笔持仓信息
- position_date: ktrader_python.position_date_enum
持仓日期
- position_cost: float
仓位成本
- use_margin: float
占用保证金
- class ktrader_python.instrument_position
基类:
ktrader_python.instrument_summary合约持仓详细
- long_position_detail: List[ktrader_python.position_entry]
逐笔多仓详情
- short_position_detail: List[ktrader_python.position_entry]
逐笔空仓详情
- class ktrader_python.product_summary
基类:
object品种交易信息
- product_id: str
品种ID
- long_margin: float
多仓保证金
- long_frozen_margin: float
多仓冻结保证金
- short_margin: float
空仓保证金
- short_frozen_margin: float
空仓冻结保证金
- total_margin: float
总保证金
- total_frozen_margin: float
总冻结保证金
- num_orders: int
订单数
- num_cancel: int
撤单数
- num_trades: int
成交数
- order_trade_ratio: float
报单成交比:
- action_commission_fee: float
申报费
- class ktrader_python.position_summary
基类:
object总持仓总结
- position_profit: float
逐日持仓利润
- close_profit: float
逐日平仓利润
- total_margin: float
总保证金
- total_commission: float
总手续费
- total_order_commission: float
总报单手续费
- frozen_margin: float
冻结保证金
- frozen_commission: float
冻结手续费
- frozen_order_commission: float
冻结报单手续费
- net_pnl: float
净利润
- max_margin: float
最大保证金占用
- num_orders: int
订单数
- num_cancel: int
撤单数
- num_trade_orders: int
有成交订单数
- num_trades: int
成交手数
- num_win_trades: int
盈利成交手数
- num_loss_trades: int
亏损成交手数
- gain_per_trade: float
平均每手盈利(逐笔)
- loss_per_trade: float
平均每手亏损(逐笔)
- gain_loss_ratio: float
盈亏比例
- win_ratio: float
盈利比例
- gross_profit_per_trade: float
平均每手毛利润(逐笔)
- net_profit_per_trade: float
平均每手净利润(逐笔)
- fees_per_trade: float
平均每手费用
- class ktrader_python.account_summary
基类:
object账户总结
- broker_id: str
券商ID
- investor_id: str
用户ID
- pre_balance: float
账户昨日权益
- balance: float
账户当前权益
- close_profit: float
逐日平仓利润
- position_profit: float
逐日持仓利润
- margin_used: float
总保证金
- total_commission: float
总手续费
- net_pnl: float
净盈亏
- net_pnl_high: float
最高净盈亏(当前交易日)
- net_pnl_low: float
最低净盈亏(当前交易日)
- frozen_cash: float
冻结现金
- frozen_margin: float
冻结保证金
- frozen_commission: float
冻结手续费
- available_cash: float
可用现金
- risk_level: float
风险度 (权益-可用现金)/权益
- max_drawdown: float
最高回撤(当前权益/最高权益 - 1.0)
- class ktrader_python.trigger_decision
基类:
object触发器决策
- should_close: bool
是否平仓
- opt_price: float
可选平仓价格, 若为零则采用对价
- class ktrader_python.position_trigger
基类:
object
- update_tick(t: ktrader_python.tick) ktrader_python.trigger_decision
更新行情
- 参数
t (tick) – 最新行情
- 返回
平仓与否及可选平仓价格
- 返回类型
trigger_decision
- class ktrader_python.ktrader_api
基类:
objectKTrader API定义
- create_order(instrument_id: str, direction: ktrader_python.direction_enum, offset: ktrader_python.offset_flag_enum, time_condition: ktrader_python.time_condition_enum, volume_condition: ktrader_python.volume_condition_enum, limit_price: float, volume: int, retriable: bool) ktrader_python.order_status
下单功能, 用户可通过该方法指定KTrader API下任意订单
- cancel_order() str
撤单功能, 撤销一未成交订单
- 参数
order (order_status) – 订单状态, 需要合法, 如下单函数返回的状态
- 返回
撤单错误, 如非空则代表撤单失败返回的错误信息)
- 返回类型
- close_position(key: ktrader_python.trade_key, price: float) ktrader_python.order_status
平掉一指定仓位
- 参数
key (trade_key) – 仓位对应成交主键, 可用key_of得到
price (float) – 平仓价格
- 返回
订单状态, 用户需检查error_code是否为0)
- 返回类型
order_status
- set_target_position(target: ktrader_python.position_target, force_override: bool = False) str
设置目标仓位
- get_target_position(instrument_id: str) ktrader_python.position_target
获取目标仓位
- 参数
instrument_id (str) – 合约ID
- 返回
合约当前仓位目标, 如未设置则调仓算法为manual
- 返回类型
position_target
- target_position_reached(instrument_id: str) bool
询问是否已达到目标仓位
注解
对于basic调仓算法, 净持仓达到目标仓位且无未成交订单则认为达到目标仓位, 对于manual调仓算法, 始终认为达到目标仓位, 对于market_on_open调仓算法, 收到开盘成交回报则认为达到目标仓位
- register_trigger(pos: ktrader_python.position_entry, trigger: ktrader_python.position_trigger) bool
注册平仓条件触发器
- 参数
pos (position_entry) – 要平仓的仓位
trigger (position_trigger) – 触发器
- 返回
是否成功注册触发器, 如参数中仓位不存在或该合约采用主动调仓算法则无法注册触发器
- 返回类型
- cancel_triggers(pos: ktrader_python.position_entry)
取消条件触发器
- 参数
pos (position_entry) – 要平仓的仓位
- get_last_tick(instrument_id: str) ktrader_python.tick
获取最新行情tick
- get_last_k_ticks(instrument_id: str, num_tick: int) List[ktrader_python.tick]
获取最新k个tick
- get_instrument_position_detail(instrument_id: str) ktrader_python.instrument_position
获得指定合约逐笔持仓信息, 调用该函数将订阅合约行情
- 参数
instrument_id (str) – 合约ID
- 返回
该合约对应详细持仓信息
- 返回类型
instrument_position
- get_instrument_summary(instrument_id: str) ktrader_python.instrument_summary
获得指定合约持仓总结, 调用该函数将订阅合约行情
- 参数
instrument_id (str) – 合约ID
- 返回
该合约对应持仓总结
- 返回类型
instrument_summary
- get_product_summary(product_id: str) ktrader_python.product_summary
获得指定品种交易信息, 包括保证金和报单成交比、申报费
- 参数
product_id (str) – 品种ID
- 返回
该品种对应交易信息
- 返回类型
instrument_summary
- get_position_summary() ktrader_python.position_summary
获取本策略全部仓位总结
- 返回
本策略持仓总结
- 返回类型
position_summary
- get_position_entry(key: ktrader_python.trade_key) ktrader_python.position_entry
获取一笔具体持仓
- 参数
key (trade_key) – 成交主键
- 返回
具体逐笔持仓信息
- 返回类型
position_entry
- get_all_position_summary() List[ktrader_python.instrument_summary]
获取全部合约持仓总结
- 返回
全部已订阅订单的持仓总结
- 返回类型
List[instrument_summary]
- get_all_position_detail() List[ktrader_python.instrument_position]
获取全部合约持仓详情
- 返回
全部已订阅合约的持仓详细信息
- 返回类型
List[instrument_position]
- get_inflight_orders(instrument_id: str) List[ktrader_python.order_status]
获取全部未完成订单
- 参数
instrument_id (str) – 按合约ID筛选, 不填为全部合约
- 返回
全部未完成订单的订单状态
- 返回类型
List[order_status]
- get_account_summary() ktrader_python.account_summary
获取账户信息
- 返回
本账户资金信息
- 返回类型
account_summary
- get_last_k_bars(instrument_id: str, window_size: int, freq: int = 1) List[ktrader_python.bar]
获取指定合约K线
- class ktrader_python.python_strategy
基类:
object策略核心
KTrader策略核心接口定义, 用户的量化策略需实现该接口
- update_config(config: str) str
更新策略配置
strategy_config/global_config.json中配置的’config’项目将会成为 策略初始化时的参数
- on_tick(t: ktrader_python.tick)
行情回调
用户需调用KTrader API的get_instrument_summary方法订阅合约行情, 当KTrader收到订阅的合约行情更新时, 会调用该方法
- 参数
t (tick) – 最新行情更新
- on_bar(b: ktrader_python.bar)
K线回调
用户需调用KTrader API的subscribe_bars方法订阅合约K线行情, 当KTrader产生新的K线行情更新时, 会调用该方法
- 参数
b (bar) – 最新K线更新
- on_order_update(update: ktrader_python.order_update)
订单更新回调
当该策略所下订单有新成交或被撤单时会调用该方法
- 参数
update (order_update) – 订单更新
- init()
初始化
策略初始化时KTrader API可用且update_config已被调用
- shutdown()
系统结束
KTrader系统结束前会调用该方法, 用户可以进行资源释放等操作
- api: ktrader_python.ktrader_api
当init被调用时, 系统会配置好api
- ktrader_python.key_of(param: ktrader_python.trade_info) ktrader_python.trade_key
获得成交主键
- 参数
param (trade_info) – 成交信息
- 返回
对应成交主键
- 返回类型
trade_key
- ktrader_python.parse_time_format(fmt: str, t: str) int
解析时间,转为Unix纳秒
- 参数
fmt (str) – strftime格式(https://zh.cppreference.com/w/c/chrono/strftime)
t (str) – 要解析的时间字符串
- 返回
解析后时间(Unix纳秒)
- 返回类型
- ktrader_python.format_time(timestamp_nano: int, fmt: str = '') str
格式化时间
- 参数
timestamp_nano (int) – 时间(Unix纳秒)
fmt (str) – strftime格式(https://zh.cppreference.com/w/c/chrono/strftime), 空字符串则为默认格式(如20210625-09:30:00.535)
- 返回
时间字符串
- 返回类型